<?php

/**************************************
CUSTOM META FIELD
***************************************/

	//metaboxes
	add_action('add_meta_boxes', 'register_canon_cmb_posts');
	add_action ('save_post', 'update_canon_cmb_posts');

	function register_canon_cmb_posts () {
		add_meta_box('canon_cmb_posts','Kodax Post Settings', 'display_canon_cmb_posts','post');
	}

	function display_canon_cmb_posts ($post) {

	/**************************************
	GET VALUES
	***************************************/

	// OPTIONS
		$default_excerpt_len = 300;

	// GENERAL
		$result_cmb_use_media_link = get_post_meta($post->ID, 'cmb_use_media_link', true);
		$result_cmb_media_link = get_post_meta($post->ID, 'cmb_media_link', true);
		$result_cmb_excerpt = get_post_meta($post->ID, 'cmb_excerpt', true);
		$result_cmb_quote_is_tweet = get_post_meta($post->ID, 'cmb_quote_is_tweet', true);
		$result_cmb_quote_by = get_post_meta($post->ID, 'cmb_quote_by', true);
		$result_cmb_alignment = get_post_meta($post->ID, 'cmb_alignment', true);
		$result_cmb_hide_from_archive = get_post_meta($post->ID, 'cmb_hide_from_archive', true);
		$result_cmb_hide_from_gallery = get_post_meta($post->ID, 'cmb_hide_from_gallery', true);
		$result_cmb_hide_feat_img = get_post_meta($post->ID, 'cmb_hide_feat_img', true);
		$result_cmb_hide_from_post_slider = get_post_meta($post->ID, 'cmb_hide_from_post_slider', true);

		$result_cmb_exist = get_post_meta($post->ID, 'cmb_exist', true);

	    //GET POST ATTACHMENTS
	    $args = array(
	        'post_type' => 'attachment',
	        'numberposts' => -1,
	        'post_status' => null,
	        'orderby' => 'title',
	        'order'  => 'ASC',
	        'post_parent' => $post->ID
	    );

	    $post_attachments = get_posts( $args );

		//defaults
		if (empty($result_cmb_exist)) {
			//$result_cmb_excerpt = mb_make_excerpt($post->post_content, $default_excerpt_len, true);
			$result_cmb_alignment = "";
		}

	/**************************************
	DISPLAY CONTENT
	***************************************/

		?>

	<!-- GENERAL -->

		<div class="option_heading">
			<span><?php _e("General", "loc_kodax_core_plugin"); ?></span>
		</div>

		<div class="options_post_format default_hidden" data-post_format="quote">
			<div class="option_item">
				<input type='checkbox' id='cmb_quote_is_tweet' name='cmb_quote_is_tweet' value='checked' <?php checked(!empty($result_cmb_quote_is_tweet)); ?>>
				<label for='cmb_quote_is_tweet'><?php _e("Display quote as a tweet", "loc_kodax_core_plugin"); ?></label><br>
			</div>

		</div>

		<div class="options_post_format default_hidden" data-post_format="0">
			<div class="option_item">
				<label for='cmb_alignment'><?php _e("Post Title Alignment", "loc_kodax_core_plugin"); ?></label><br>
				<select id="cmb_alignment" name="cmb_alignment"> 
	     			<option value="" <?php if (isset($result_cmb_alignment)) {if ($result_cmb_alignment == "") echo "selected='selected'";} ?>><?php _e("Auto-align", "loc_kodax_core_plugin"); ?></option> 
	     			<option value="left" <?php if (isset($result_cmb_alignment)) {if ($result_cmb_alignment == "left") echo "selected='selected'";} ?>><?php _e("Post title left", "loc_kodax_core_plugin"); ?></option> 
	     			<option value="right" <?php if (isset($result_cmb_alignment)) {if ($result_cmb_alignment == "right") echo "selected='selected'";} ?>><?php _e("Post title right", "loc_kodax_core_plugin"); ?></option> 
				</select> 
			</div>
		</div>

		<div class="option_item">
			<input type='checkbox' id='cmb_use_media_link' name='cmb_use_media_link' value='checked' <?php checked(!empty($result_cmb_use_media_link)); ?>>
			<label for='cmb_use_media_link'><?php _e("Use embeddable media instead of featured image", "loc_kodax_core_plugin"); ?> : </label>
			<input type='text' id='cmb_media_link' name='cmb_media_link' class='widefat' value='<?php if (!empty($result_cmb_media_link)) echo $result_cmb_media_link; ?>'>
			<span class="item_hint float_right">(<?php _e("Media embed code. Remember to adjust width. Set to 100% for auto. Also set height or remove for auto.", "loc_kodax_core_plugin"); ?>)</span><br><br>
		</div>

		<div class="option_item">
			<label for='cmb_excerpt'><?php _e("Excerpt / Quote", "loc_kodax_core_plugin"); ?></label><br>
			<textarea id='cmb_excerpt' name='cmb_excerpt' class='widefat' rows='5'><?php if (!empty($result_cmb_excerpt)) echo $result_cmb_excerpt; ?></textarea>
			<button type="button" name="button_generate_excerpt" id='button_generate_excerpt' class="button-secondary auto_generate" value="<?php echo mb_make_excerpt($post->post_content, $default_excerpt_len, true); ?>">Auto-generate</button>
			<span class="item_hint float_right">&lt;p class="lead"&gt;<?php _e("Use this tag for lead text", "loc_kodax_core_plugin"); ?>&lt;/p&gt;</span>
		</div>

		<div class="option_item">
			<input type='checkbox' id='cmb_hide_from_archive' name='cmb_hide_from_archive' value='checked' <?php checked(!empty($result_cmb_hide_from_archive)); ?>>
			<label for='cmb_hide_from_archive'><?php _e("Hide from blog", "loc_kodax_core_plugin"); ?></label><br>
		</div>

		<div class="option_item">
			<input type='checkbox' id='cmb_hide_from_gallery' name='cmb_hide_from_gallery' value='checked' <?php checked(!empty($result_cmb_hide_from_gallery)); ?>>
			<label for='cmb_hide_from_gallery'><?php _e("Hide from gallery", "loc_kodax_core_plugin"); ?></label><br>
		</div>

	<!-- POST SLIDER -->

		<div class="option_heading">
			<span><?php _e("Post slider", "loc_kodax_core_plugin"); ?></span>
		</div>

		<?php 
			if ((count($post_attachments) < 1) && !has_post_thumbnail($post->ID) ) {
			?>				
				<div class="option_item">
					<i><?php _e("No images have been attached to your post", "loc_kodax_core_plugin"); ?></i>
				</div>
			<?php
			} else {
			?>

				<div class="option_item">
					<?php _e("Hide from post slider", "loc_kodax_core_plugin"); ?>: <br><br>

					<?php 
						if (has_post_thumbnail($post->ID)) {
						?>
							<input type='checkbox' id='cmb_hide_feat_img' name='cmb_hide_feat_img' value='checked' <?php checked(!empty($result_cmb_hide_feat_img)); ?>>
							<label for='cmb_hide_feat_img'>Featured image</label><br><br>
								
						<?php
						}

						for ($i = 0; $i < count($post_attachments); $i++) {  
							if (get_post_thumbnail_id($post->ID) != $post_attachments[$i]->ID) {
								?>
									<input type='checkbox' id='cmb_hide_from_post_slider' name='cmb_hide_from_post_slider[<?php echo $post_attachments[$i]->ID; ?>]' value='checked' <?php checked(isset($result_cmb_hide_from_post_slider[$post_attachments[$i]->ID])); ?>>
									<label for='cmb_hide_from_post_slider'><?php echo $post_attachments[$i]->post_title; ?></label><br>

								<?php
								}
							}

					?>

				</div>

			<?php
			}


		?>


		<!-- add nonce -->
		<input type="hidden" name="cmb_nonce" value="<?php echo wp_create_nonce(basename(__FILE__)); ?>" />
		<input type="hidden" name="cmb_exist" value="true" />






		<?php	
	}



/**************************************
UPDATE
***************************************/

	function update_canon_cmb_posts ($post_id) {
		// avoid activation on irrelevant admin pages
		if (!isset($_POST['cmb_nonce'])) {
			return false;		
		}

		// verify nonce.    
		if (!wp_verify_nonce($_POST['cmb_nonce'], basename(__FILE__)) || !isset($_POST['cmb_nonce'])) {
			return false;
		}

		if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
			return;
		} else {

		//GENERAL
			if (isset($_POST['cmb_use_media_link'])) { update_post_meta($post_id, 'cmb_use_media_link', $_POST['cmb_use_media_link']); } else { update_post_meta($post_id, 'cmb_use_media_link', null); };
			if (isset($_POST['cmb_media_link'])) { update_post_meta($post_id, 'cmb_media_link', $_POST['cmb_media_link']); } else { update_post_meta($post_id, 'cmb_media_link', null); };
			if (isset($_POST['cmb_excerpt'])) { update_post_meta($post_id, 'cmb_excerpt', $_POST['cmb_excerpt']); } else { update_post_meta($post_id, 'cmb_excerpt', null); };
			if (isset($_POST['cmb_quote_by'])) { update_post_meta($post_id, 'cmb_quote_by', $_POST['cmb_quote_by']); } else { update_post_meta($post_id, 'cmb_quote_by', null); };
			if (isset($_POST['cmb_quote_is_tweet'])) { update_post_meta($post_id, 'cmb_quote_is_tweet', $_POST['cmb_quote_is_tweet']); } else { update_post_meta($post_id, 'cmb_quote_is_tweet', null); };
			if (isset($_POST['cmb_alignment'])) { update_post_meta($post_id, 'cmb_alignment', $_POST['cmb_alignment']); } else { update_post_meta($post_id, 'cmb_alignment', null); };
			if (isset($_POST['cmb_hide_from_archive'])) { update_post_meta($post_id, 'cmb_hide_from_archive', $_POST['cmb_hide_from_archive']); } else { update_post_meta($post_id, 'cmb_hide_from_archive', null); };
			if (isset($_POST['cmb_hide_from_gallery'])) { update_post_meta($post_id, 'cmb_hide_from_gallery', $_POST['cmb_hide_from_gallery']); } else { update_post_meta($post_id, 'cmb_hide_from_gallery', null); };
			if (isset($_POST['cmb_hide_feat_img'])) { update_post_meta($post_id, 'cmb_hide_feat_img', $_POST['cmb_hide_feat_img']); } else { update_post_meta($post_id, 'cmb_hide_feat_img', null); };
			if (isset($_POST['cmb_hide_from_post_slider'])) { update_post_meta($post_id, 'cmb_hide_from_post_slider', $_POST['cmb_hide_from_post_slider']); } else { update_post_meta($post_id, 'cmb_hide_from_post_slider', null); };

			if (isset($_POST['cmb_exist'])) { update_post_meta($post_id, 'cmb_exist', $_POST['cmb_exist']); } else { update_post_meta($post_id, 'cmb_exist', null); };

		}

	}


